package com.zzyk.main.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zzyk.main.mapper.DeviceExtraDataMapper;
import com.zzyk.main.mapper.DeviceHarmonicTotalMapper;
import com.zzyk.main.mapper.DeviceMapper;
import com.zzyk.main.mapper.DeviceOriginDataMapper;
import com.zzyk.main.model.pojo.Device;
import com.zzyk.main.model.pojo.DeviceExtraData;
import com.zzyk.main.model.pojo.DeviceOriginData;
import com.zzyk.main.model.vo.DateAndString;
import com.zzyk.main.model.vo.DeviceOriginDataVO;
import com.zzyk.main.service.DeviceOriginDataService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Log4j2
public class DeviceOriginDataServiceImpl implements DeviceOriginDataService {
    @Resource
    private DeviceOriginDataMapper deviceOriginDataMapper;
    @Resource
    private DeviceExtraDataMapper deviceExtraDataMapper;
    @Resource
    private DeviceHarmonicTotalMapper deviceHarmonicTotalMapper;
    @Resource
    private DeviceMapper deviceMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return deviceOriginDataMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(DeviceOriginData record) {
        return deviceOriginDataMapper.insert(record);
    }

    @Override
    public int insertSelective(DeviceOriginData record) {
        return deviceOriginDataMapper.insertSelective(record);
    }

    @Override
    public DeviceOriginData selectByPrimaryKey(Integer id) {
        return deviceOriginDataMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(DeviceOriginData record) {
        return deviceOriginDataMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(DeviceOriginData record) {
        return deviceOriginDataMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<DateAndString> findDeviceOriginData(Integer eId, Integer dId, String param, String beginDate, String endDate, Integer type) {
        Date bDate;
        Date eDate;
        if (StringUtils.isBlank(beginDate) && StringUtils.isBlank(endDate)) {
            bDate = DateUtil.beginOfDay(DateUtil.date());
        } else if (StringUtils.isBlank(beginDate) && StringUtils.isNotBlank(endDate)) {
            bDate = DateUtil.offsetMonth(DateUtil.parseDate(endDate), -1);
        } else {
            bDate = DateUtil.parseDateTime(beginDate);
        }
        if (StringUtils.isBlank(endDate) && StringUtils.isBlank(beginDate)) {
            eDate = DateUtil.parseDateTime(DateUtil.now());
        } else if (StringUtils.isBlank(endDate) && StringUtils.isNotBlank(beginDate)) {
            eDate = DateUtil.offsetMonth(bDate, 1);
        } else {
            eDate = DateUtil.parseDateTime(endDate);
        }
        /*    {value: 'distortionVA', name: 'A相电压总谐波畸变率'},
    {value: 'distortionVB', name: 'B相电压总谐波畸变率'},
    {value: 'distortionVC', name: 'C相电压总谐波畸变率'},
    {value: 'distortionIA', name: 'A相电流总谐波畸变率'},
    {value: 'distortionIB', name: 'B相电流总谐波畸变率'},
    {value: 'distortionIC', name: 'C相电流总谐波畸变率'},*/
        if ("distortionVA".equals(param) || "distortionVB".equals(param) || "distortionVC".equals(param) || "distortionIA".equals(param) || "distortionIB".equals(param) || "distortionIC".equals(param)) {
            /*去查询别的表*/
            return deviceHarmonicTotalMapper.selectByEIDAndDIDAndParam(eId, dId, param, bDate, eDate);
        }
        return deviceOriginDataMapper.selectByEIDAndDIDAndParam(eId, dId, param, bDate, eDate);
    }

    @Override
    public List<DeviceOriginDataVO> findDeviceOriginDataTest(Integer eId, Integer dId, String param, String beginDate, String endDate, Integer type) {
        Date bDate;
        Date eDate;
        if (StringUtils.isBlank(beginDate) && StringUtils.isBlank(endDate)) {
            bDate = DateUtil.beginOfDay(DateUtil.date());
        } else if (StringUtils.isBlank(beginDate) && StringUtils.isNotBlank(endDate)) {
            bDate = DateUtil.offsetMonth(DateUtil.parseDateTime(endDate), -1);
        } else {
            bDate = DateUtil.parseDateTime(beginDate);
        }
        if (StringUtils.isBlank(endDate) && StringUtils.isBlank(beginDate)) {
            eDate = DateUtil.date();
        } else if (StringUtils.isBlank(endDate) && StringUtils.isNotBlank(beginDate)) {
            eDate = DateUtil.offsetMonth(DateUtil.parseDateTime(beginDate), -1);
        } else {
            eDate = DateUtil.parseDateTime(endDate);
        }
        return deviceOriginDataMapper.selectByEIDAndDIDAndParamTest(eId, dId, param, bDate, eDate);
    }

    @Override
    public void migrate(List<DeviceOriginData> deviceOriginDataList) throws InterruptedException {
        try {
            DateTime begin = DateUtil.date();
            if (ObjectUtil.isNull(deviceOriginDataList) || deviceOriginDataList.isEmpty()) {
                deviceOriginDataList = deviceOriginDataMapper.selectall();
            }
            for (DeviceOriginData deviceOriginData : deviceOriginDataList) {
                Device device = deviceMapper.selectcOneBayClientId(deviceOriginData.getClientId());
                if (ObjectUtil.isNotNull(device)) {
                    deviceOriginData.setDeviceId(device.getId());
                    DeviceExtraData deviceExtraData = deviceExtraDataMapper.selectByPrimaryKey(deviceOriginData.getId());
                    if (ObjectUtil.isNotNull(deviceExtraData)) {
                        deviceOriginData.setWork(deviceExtraData.getWork());
                        deviceOriginData.setVoltageDeviation(deviceExtraData.getVoltageDeviation());
                        deviceOriginData.setFrequencyDeviation(deviceExtraData.getFrequencyDeviation());
                        deviceOriginData.setUnblance(deviceExtraData.getUnblance());
                        deviceOriginData.setLoadRate(deviceExtraData.getLoadRate());
                        deviceOriginData.setHealth(deviceExtraData.getHealth());
                        deviceOriginData.setPositionDeviation(deviceExtraData.getPositionDeviation());
                        deviceOriginData.setPowerFactor(deviceExtraData.getPowerFactor());
                    }
                } else {
                    deviceOriginData.setDeviceId(0);
                }
                deviceOriginDataMapper.updateByPrimaryKeySelective(deviceOriginData);
            }
            DateTime end = DateUtil.date();
            Long between = DateUtil.between(begin, end, DateUnit.SECOND);
            log.info("{}条数据迁移完成，开始时间{}，结束时间{}，用时{}秒", deviceOriginDataList.size(), begin, end, between);
            List<DeviceOriginData> list = deviceOriginDataMapper.selectall();
            if (list.size() > 0) {
                migrate(list);
            } else {
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Thread.sleep(5000);
            migrate(null);
        }
    }

}
